from django.db import models

"""项目、人员、权限配置"""

class UserInfo(models.Model):
    """
    用户信息
    """
    username = models.CharField(verbose_name="用户名", max_length=32, db_index=True)  # db_index=True 索引
    email = models.EmailField(verbose_name="邮箱", max_length=32)
    mobile_phone = models.CharField(verbose_name="手机号码", max_length=32)
    password = models.CharField(verbose_name="密码", max_length=32)
    create_datetime = models.DateTimeField(verbose_name='加入时间', auto_now_add=True)
    login_time = models.DateTimeField(verbose_name='当前登录时间')

    def __str__(self):
        return self.username

class Project(models.Model):
    """
    项目表
    """
    name = models.CharField(verbose_name='项目名', max_length=32)
    desc = models.CharField(verbose_name='项目描述', max_length=255, null=True, blank=True)
    # join_count = models.SmallIntegerField(verbose_name='参与人数', default=1)
    creator_id = models.ForeignKey(verbose_name='创建者', to='UserInfo', on_delete=models.SET_NULL, null=True)
    create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)

class ProjectMember(models.Model):
    """
    项目成员
    """
    CHOICES = (
        ('超级管理员', '超级管理员'),
        ('开发人员', '开发人员'),
        ('测试人员', '测试人员')
    )
    user_permission = models.CharField(max_length=50, verbose_name='用户权限', choices=CHOICES)
    member_project = models.ForeignKey(Project, verbose_name='所属项目', null=True, on_delete=models.SET_NULL)
    project_user = models.ForeignKey(UserInfo, verbose_name='项目成员', null=True, on_delete=models.SET_NULL )

    def __str__(self):
        return self.user_permission

    class Meta:
        verbose_name = '项目成员'
        verbose_name_plural = '项目成员'

class OperationLog(models.Model):
    """
    操作日志
    """
    opreation_type = models.IntegerField(verbose_name='操作类型', null=False, blank=False)
    opreation_content = models.CharField(verbose_name='项目描述', max_length=255, null=False, blank=False)
    opreation_user = models.ForeignKey(verbose_name='操作人员', to='UserInfo', null=True, on_delete=models.SET_NULL)
    opreation_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)

    def __str__(self):
        return self.opreation_type

class ProjectDynamic(models.Model):
    """
    项目动态
    """
    opreation_project = models.ForeignKey(Project, on_delete=models.CASCADE, verbose_name='所属项目')
    opreation_type = models.CharField(max_length=50, verbose_name='操作类型')
    operation_object = models.CharField(max_length=50, verbose_name='操作对象')
    opreation_user = models.ForeignKey(UserInfo, blank=True, null=True, on_delete=models.SET_NULL, verbose_name='操作人')
    opreation_time = models.DateTimeField(max_length=128, verbose_name='操作时间')
    opreation_desc = models.CharField(max_length=1024, blank=True, null=True,  verbose_name='描述')

    def __str__(self):
        return self.opreation_type

    class Meta:
        verbose_name = '项目动态'
        verbose_name_plural = '项目动态'

"""接口相关--全局配置"""

class GlobalHost(models.Model):
    """
    host域名
    """
    #地址所属项目，若为空则全部项目都能显示
    project_id = models.ForeignKey(Project, on_delete=models.CASCADE, null=True, verbose_name='项目')
    name = models.CharField(max_length=50, verbose_name='名称')
    host = models.CharField(max_length=1024, verbose_name='Host地址')
    desc = models.CharField(max_length=1024, blank=True, null=True, verbose_name='描述')
    status = models.BooleanField(default=True, verbose_name='状态')

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = 'HOST'
        verbose_name_plural = 'HOST管理'

class GlobalHeader(models.Model):
    """
    全局 heard
    """
    project_id = models.ForeignKey(Project, on_delete=models.CASCADE, null=True, verbose_name='项目')
    name = models.CharField(max_length=1024, verbose_name="标签")
    value = models.CharField(max_length=1024, blank=True, null=True, verbose_name='内容')
    desc = models.CharField(max_length=200, blank=True, null=True, verbose_name='描述')

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '请求头'
        verbose_name_plural = '请求头管理'

class ApiGroup(models.Model):
    """
    接口一级分组
    """
    project_id = models.ForeignKey(Project, on_delete=models.CASCADE, verbose_name='项目')
    group_name = models.CharField(max_length=50, verbose_name='接口分组名称')

    def __str__(self):
        return self.group_name

    class Meta:
        verbose_name = '接口分组'
        verbose_name_plural = '接口分组'

"""接口相关--接口配置"""

HTTP_CHOICE = (
    ('HTTP', 'HTTP'),
    ('HTTPS', 'HTTPS')
)

REQUEST_TYPE_CHOICE = (
    ('POST', 'POST'),
    ('GET', 'GET'),
    ('PUT', 'PUT'),
    ('DELETE', 'DELETE')
)

REQUEST_PARAMETER_TYPE_CHOICE = (
    ('form-data', '表单(form-data)'),
    ('raw', '源数据(raw)'),
    ('Restful', 'Restful')
)

PARAMETER_TYPE_CHOICE = (
    ('text', 'text'),
    ('file', 'file')
)

HTTP_CODE_CHOICE = (
    ('200', '200'),
    ('404', '404'),
    ('400', '400'),
    ('502', '502'),
    ('500', '500'),
    ('302', '302'),
)

EXAMINE_TYPE_CHOICE = (
    ('no_check', '不校验'),
    ('only_check_status', '校验http状态'),
    ('json', 'JSON校验'),
    ('entirely_check', '完全校验'),
    ('Regular_check', '正则校验'),
)

UNIT_CHOICE = (
    ('m', '分'),
    ('h', '时'),
    ('d', '天'),
    ('w', '周'),
)

RESULT_CHOICE = (
    ('PASS', '成功'),
    ('FAIL', '失败'),
)

class ApiInfo(models.Model):
    """
    接口信息
    """
    user_id = models.ForeignKey(UserInfo, on_delete=models.SET_NULL, null=True, max_length=50, verbose_name='更新人', )
    project_id = models.ForeignKey(Project, on_delete=models.CASCADE, verbose_name='所属项目')
    group_id = models.ForeignKey(ApiGroup, blank=True, null=True, on_delete=models.CASCADE, verbose_name='所属分组')
    # api_id_response = models.ManyToManyField(to='self', verbose_name='关联参数')#关联对应接口的响应值
    host_id = models.ForeignKey(GlobalHost, blank=True, null=True, on_delete=models.SET_NULL, verbose_name='HOST地址')

    api_name = models.CharField(max_length=50, verbose_name='接口名称')
    http_type = models.CharField(max_length=50, default='HTTP', verbose_name='http/https', choices=HTTP_CHOICE)
    request_type = models.CharField(max_length=50, verbose_name='请求方式', choices=REQUEST_TYPE_CHOICE)
    api_address = models.CharField(max_length=1024, verbose_name='接口地址')
    # request_parameter_type = models.CharField(max_length=50, verbose_name='请求参数格式', choices=REQUEST_PARAMETER_TYPE_CHOICE)
    api_status = models.BooleanField(default=True, verbose_name='状态')
    mock_status = models.BooleanField(default=False, verbose_name="mock状态")
    mock_code = models.CharField(max_length=50, blank=True, null=True, verbose_name='HTTP状态', choices=HTTP_CODE_CHOICE)
    mock_data = models.TextField(blank=True, null=True, verbose_name='mock内容')
    last_update_time = models.DateTimeField(auto_now=True, verbose_name='最近更新时间')
    api_desc = models.CharField(max_length=1024, blank=True, null=True, verbose_name='描述')


    def __str__(self):
        return self.api_name

    class Meta:
        verbose_name = '接口'
        verbose_name_plural = '接口管理'


class ApiHeader(models.Model):
    """
    请求头信息
    """
    api_id = models.ForeignKey(ApiInfo, on_delete=models.CASCADE, verbose_name="所属接口",)
    header_name = models.CharField(max_length=1024, verbose_name="标签")
    header_value = models.CharField(max_length=1024, blank=True, null=True, verbose_name='内容')
    header_required = models.BooleanField(default=True, verbose_name="是否必填")
    header_type = models.CharField(default="String", max_length=1024, verbose_name='参数类型',
                                  choices=(('Int', 'Int'), ('String', 'String')))
    header_desc = models.CharField(max_length=1024, blank=True, null=True, verbose_name="描述")

    def __str__(self):
        return self.header_name

    class Meta:
        verbose_name = '请求头'
        verbose_name_plural = '请求头管理'


class ApiQuery(models.Model):
    """
    请求参数
    """
    api_id = models.ForeignKey(ApiInfo, on_delete=models.CASCADE, verbose_name="所属接口")
    query_name = models.CharField(max_length=1024, verbose_name="参数名")
    query_value = models.CharField(max_length=1024, blank=True, null=True, verbose_name='参数值')
    query_type = models.CharField(default="String", max_length=1024, verbose_name='参数类型', choices=(('Int', 'Int'), ('String', 'String')))
    query_required = models.BooleanField(default=True, verbose_name="是否必填")
    query_restrict = models.CharField(max_length=1024, blank=True, null=True, verbose_name="输入限制")
    query_desc = models.CharField(max_length=1024, blank=True, null=True, verbose_name="描述")

    def __str__(self):
        return self.query_name

    class Meta:
        verbose_name = '请求参数'
        verbose_name_plural = '请求参数管理'


class ApiBody(models.Model):

    api_id = models.OneToOneField(ApiInfo, on_delete=models.CASCADE, verbose_name="所属接口")
    json_data = models.TextField(blank=True, null=True, verbose_name='内容')

    class Meta:
        verbose_name = 'Post请求参数'


class ApiResponse(models.Model):
    """
    请求响应参数
    """
    api_id = models.ForeignKey(ApiInfo, on_delete=models.CASCADE, verbose_name="所属接口")
    response_name = models.CharField(max_length=1024, verbose_name="参数名")
    response_value = models.CharField(max_length=1024, blank=True, null=True, verbose_name='参数值')
    response_type = models.CharField(default="String", max_length=1024, verbose_name='参数类型', choices=(('Int', 'Int'), ('String', 'String')))
    response_required = models.BooleanField(default=True, verbose_name="是否必含")
    response_desc = models.CharField(max_length=1024, blank=True, null=True, verbose_name="描述")

    def __str__(self):
        return self.response_name

    class Meta:
        verbose_name = '返回参数'
        verbose_name_plural = '返回参数管理'


class APIRequestHistory(models.Model):
    """
    接口请求历史
    """
    api_id = models.ForeignKey(ApiInfo, on_delete=models.CASCADE, verbose_name='接口')
    request_time = models.DateTimeField(auto_now_add=True, verbose_name='请求时间')
    request_type = models.CharField(max_length=50, verbose_name='请求方法')
    request_address = models.CharField(max_length=1024, verbose_name='请求地址')
    request_code = models.CharField(max_length=50, verbose_name='HTTP状态')

    def __unicode__(self):
        return self.request_address

    class Meta:
        verbose_name = '接口请求历史'
        verbose_name_plural = '接口请求历史'


class ApiOperationHistory(models.Model):
    """
    API操作历史
    """
    api_id = models.ForeignKey(ApiInfo, on_delete=models.CASCADE, verbose_name='接口')
    user_id = models.ForeignKey(UserInfo, on_delete=models.SET_NULL, null=True, max_length=50, verbose_name='用户姓名')
    operation_time = models.DateTimeField(auto_now_add=True, verbose_name='操作时间')
    operation_content = models.CharField(max_length=1024, blank=True, null=True, verbose_name='操作内容')

    def __unicode__(self):
        return self.operation_content

    class Meta:
        verbose_name = '接口操作历史'
        verbose_name_plural = '接口操作历史'
